package cn.elead.chaos.sso.server.web.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.elead.chaos.excel.ExcelUtil;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.shiro.util.ShiroUtils;
import cn.elead.chaos.sso.server.constants.UserConstants;
import cn.elead.chaos.sso.server.web.system.domain.SysDictType;
import cn.elead.chaos.sso.server.web.system.domain.Ztree;
import cn.elead.chaos.sso.server.web.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictTypeController extends BaseController {
	private String prefix = "system/dict/type";

	@Autowired
	private ISysDictTypeService dictTypeService;

	@RequiresPermissions("system:dict:view")
	@GetMapping()
	public String dictType() {
		return prefix + "/type";
	}

	@ResponseBody
	@UnifiedReturn
	@PostMapping("/list")
	@RequiresPermissions("system:dict:list")
	public TableDataInfo<SysDictType> list(SysDictType dictType) {
		startPage();
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		return getDataTable(list);
	}

	@ResponseBody
	@PostMapping("/export")
	@RequiresPermissions("system:dict:export")
	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	public String export(SysDictType dictType) {
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
		return util.exportExcel(list, "字典类型");
	}

	/**
	 * 新增字典类型
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存字典类型
	 */
	@ResponseBody
	@PostMapping("/add")
	@RequiresPermissions("system:dict:add")
	@Log(title = "字典类型", businessType = BusinessType.INSERT)
	public void addSave(@Validated SysDictType dict) {
		if (UserConstants.DICT_TYPE_NOT_UNIQUE == dictTypeService.checkDictTypeUnique(dict)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增字典'" + dict.getDictName() + "'失败，字典类型已存在"));
		}
		dict.setCreateBy(ShiroUtils.getLoginName());
		dictTypeService.insertDictType(dict);
	}

	/**
	 * 修改字典类型
	 */
	@GetMapping("/edit/{dictId}")
	public String edit(@PathVariable("dictId") Long dictId, ModelMap mmap) {
		mmap.put("dict", dictTypeService.selectDictTypeById(dictId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存字典类型
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("system:dict:edit")
	@Log(title = "字典类型", businessType = BusinessType.UPDATE)
	public void editSave(@Validated SysDictType dict) {
		if (UserConstants.DICT_TYPE_NOT_UNIQUE == dictTypeService.checkDictTypeUnique(dict)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改字典'" + dict.getDictName() + "'失败，字典类型已存在"));
		}
		dict.setUpdateBy(ShiroUtils.getLoginName());
		dictTypeService.updateDictType(dict);
	}

	@ResponseBody
	@PostMapping("/remove")
	@RequiresPermissions("system:dict:remove")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	public void remove(String ids) throws Exception {
		dictTypeService.deleteDictTypeByIds(ids);
	}

	/**
	 * 查询字典详细
	 */
	@GetMapping("/detail/{dictId}")
	@RequiresPermissions("system:dict:list")
	public String detail(@PathVariable("dictId") Long dictId, ModelMap mmap) {
		mmap.put("dict", dictTypeService.selectDictTypeById(dictId));
		mmap.put("dictList", dictTypeService.selectDictTypeAll());
		return "system/dict/data/data";
	}

	/**
	 * 校验字典类型
	 */
	@ResponseBody
	@PostMapping("/checkDictTypeUnique")
	public int checkDictTypeUnique(SysDictType dictType) {
		return dictTypeService.checkDictTypeUnique(dictType);
	}

	/**
	 * 选择字典树
	 */
	@GetMapping("/selectDictTree/{columnId}/{dictType}")
	public String selectDeptTree(@PathVariable("columnId") Long columnId, @PathVariable("dictType") String dictType, ModelMap mmap) {
		mmap.put("columnId", columnId);
		mmap.put("dict", dictTypeService.selectDictTypeByType(dictType));
		return prefix + "/tree";
	}

	/**
	 * 加载字典列表树
	 */
	@ResponseBody
	@GetMapping("/treeData")
	public List<Ztree> treeData() {
		List<Ztree> ztrees = dictTypeService.selectDictTree(new SysDictType());
		return ztrees;
	}
}
